package com.lagou.homework143;

import java.io.*;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

public class CopyTask implements Callable {

    private static final String sourcePath = "E:\\homework142";
    private static final String targetPath = "E:\\homework142_copy";

    private static List<String> pathList = new LinkedList<String>();

    public static LinkedList<File> filesPath = new LinkedList<File>();

    @Override
    public Object call() {
        //创建目标文件夹
        if (!new File(targetPath).exists())
            new File(targetPath).mkdirs();

        System.out.println("复制前 遍历【源文件夹】 " + sourcePath);
        find(sourcePath);
        System.out.println("-----------------------");
        System.out.println("复制前 遍历【目标文件夹】 " + targetPath);
        find(targetPath);
        System.out.println("-----------------------");

        //用绝对路径挨个复制/创建
        for (String path : pathList) {
            try {
                copy(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        System.out.println("复制后 遍历【目标文件夹】 " + targetPath);
        find(targetPath);

        return null;
    }

    private static void copy(String path) throws IOException {
        String newPath = path.replace(sourcePath, targetPath);
        File file = new File(path);
        File newFile = new File(newPath);

        //根据已存在的源文件判断属性
        if (file.isFile()) { //文件，copy
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(path));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newPath));
            byte[] bytes = new byte[1024];
            int res = 0;
            while ((res = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, res);
            }
        } else newFile.mkdir(); //目录，创建目标文件夹

    }

    private static void copy2(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length > 0) {
                for (File file2 : files) {
                    System.out.println(file2.getAbsolutePath());
                    if (file2.isDirectory()) {
                        find(file2.getAbsolutePath());
                    }
                }
            }
        }
    }

    private List<String> ergodic(File file, List<String> resultFileName) {
        File[] files = file.listFiles();
        if (files == null) return resultFileName;// 判断目录下是不是空的
        for (File f : files) {
            if (f.isDirectory()) {// 判断是否文件夹
                resultFileName.add(f.getPath());
                ergodic(f, resultFileName);// 调用自身,查找子目录
            } else
                resultFileName.add(f.getPath());
        }
        return resultFileName;
    }

    public static void find(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length > 0) {
                for (File file2 : files) {
                    System.out.println(file2.getAbsolutePath());
                    pathList.add(file2.getAbsolutePath());
                    if (file2.isDirectory()) {
                        find(file2.getAbsolutePath());
                    }
                }
            }
//            else System.out.println(file.getAbsolutePath() + ", 没有文件或目录~");
        } else {
            System.out.println("文件不存在!");
        }
    }

}
